package com.gdones.gpt.bus.modes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyPager;
import com.gdones.common.core.dto.MyResult;
import com.gdones.gpt.bus.modes.dto.GptModes;
import com.gdones.gpt.bus.modes.mapper.GptModesMapper;
import com.gdones.gpt.bus.modes.service.IGptModesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.gpt.bus.webAccount.dto.GptWebaccount;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-06-01
 */
@Service
public class GptModesServiceImpl extends ServiceImpl<GptModesMapper, GptModes> implements IGptModesService {

    @Override
    public MyResult pages(GptModes dto, MyPageBand pageBand) {

        MyResult result = new MyResult();

        GptModesMapper gptModesMapper = getBaseMapper();

        IPage<GptModes> pages = gptModesMapper.pages(PageDTO.of(pageBand.getPage(), pageBand.getSize()), dto);

        // 根据分页的结果封装返回数据
        MyPager<GptModes> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(pageBand.getPage());
        pager.setPageSize(pageBand.getSize());
        pager.setRows(pages.getRecords());

        result.setData(pager);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult mylist(GptModes dto) {

        MyResult result = new MyResult();

        GptModesMapper gptModesMapper = getBaseMapper();

        List<GptModes> list = gptModesMapper.myList(dto);

        result.setData(list);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult getByID(String id) {
        MyResult result = new MyResult();

        GptModesMapper gptModesMapper = getBaseMapper();

        GptModes gptModes = gptModesMapper.selectById(id);

        if(gptModes!=null){
            result.setMsg("查询成功！");
            result.setData(gptModes);
        }else{
            result.setMsg("查询失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult add(GptModes dto) {
        MyResult result = new MyResult();

        GptModesMapper gptModesMapper = getBaseMapper();

        // 查询出已有的列表
        QueryWrapper<GptModes> queryWrapper = new QueryWrapper<>();
        List<GptModes> gptModes = gptModesMapper.selectList(queryWrapper);
        boolean isHave = false;
        for(GptModes modes : gptModes){
            if(modes.getName().equals(dto.getName())){
                isHave = true;
                break;
            }
        }

        if(isHave){
            result.setCode("9999");
            result.setMsg("模型名称重复！");
            return result;
        }

        int insert = gptModesMapper.insert(dto);

        if(insert>0){
            result.setMsg("模型添加成功！");
        }else{
            result.setCode("9999");
            result.setMsg("模型添加失败！");
        }

        return result;
    }

    @Override
    public MyResult update(GptModes dto) {
        MyResult result = new MyResult();

        GptModesMapper gptModesMapper = getBaseMapper();

        // 查询除自己之外的其他模型数据
        QueryWrapper<GptModes> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("modelsID",dto.getModelsID());

        List<GptModes> gptModes = gptModesMapper.selectList(queryWrapper);

        boolean isHave = false;
        for(GptModes modes : gptModes){
            if(modes.getName().equals(dto.getName())){
                isHave = true;
                break;
            }
        }

        if(isHave){
            result.setCode("9999");
            result.setMsg("模型名称重复！");
            return result;
        }

        int i = gptModesMapper.updateById(dto);

        if(i>0){
            result.setMsg("模型修改成功！");
        }else{
            result.setCode("9999");
            result.setMsg("模型修改失败！");
        }

        return result;
    }

    @Override
    public MyResult deletes(String ids) {
        MyResult result = new MyResult();

        GptModesMapper gptModesMapper = getBaseMapper();

        String[] split = ids.split(",");

        List<String> list = Arrays.asList(split);

        int i = gptModesMapper.deleteBatchIds(list);

        if(i>0){
            result.setMsg("模型删除成功！");
        }else{
            result.setCode("9999");
            result.setMsg("模型删除失败！");
        }

        return result;
    }


}
